package com.rivulet.foreign.service.impl;

import com.rivulet.foreign.mapper.ScholarshipMapper;
import com.rivulet.foreign.pojo.Scholarship;
import com.rivulet.foreign.service.ScholarshipService;
import com.rivulet.foreign.tools.Results;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * {实现奖学金功能接口}
 *
 * @author xujiac
 * @create 2021/8/18
 * @since 1.0.0
 */

@Service
public class ScholarshipServiceImpl implements ScholarshipService {

    final ScholarshipMapper scholarshipMapper;

    public ScholarshipServiceImpl(ScholarshipMapper scholarshipMapper) {
        this.scholarshipMapper = scholarshipMapper;
    }

    @Override
    public Results checkYetScholarship() {
        Results<List<Scholarship>> results=new Results<>(400,null);
        try {
            results.setData(scholarshipMapper.getYetScholarship());
            results.setCode(100);
        }catch (Exception e){
            results.setMsg("当前无待审批奖学金申请");
            e.printStackTrace();
        }
        return results;
    }

    @Override
    public Results setScholarship(Scholarship scholarship) {
        Results results=new Results<>(400,null);
        try {
            if(scholarshipMapper.getScholarshipById(scholarship.getUser_id())!=null){
                scholarshipMapper.delScholarship(scholarship.getUser_id());
                results.setMsg("原申请信息已覆盖");
            }
            scholarshipMapper.setScholarship(
                    scholarship.getUser_id(),
                    scholarship.getScholarship_type(),
                    scholarship.getCurriculum_number(),
                    scholarship.getAverage_credit(),
                    scholarship.getGpa(),
                    scholarship.getReason(),
                    scholarship.getBonus_items_body(),
                    scholarship.getBonus_items_practice()
            );
            results.setCode(100);
        }catch (Exception e){
            results.setMsg("操作异常，请重试");
            e.printStackTrace();
        }
        return results;
    }

    @Override
    public Results dealScholarship(Scholarship scholarship) {
        Results results=new Results<>(400,null);
        try {
            Scholarship ship=scholarshipMapper.getScholarshipById(scholarship.getUser_id());
            if(ship.getIs_yes()!=null){
                results.setMsg("该申请已审批！！！");
                return results;
            }
            scholarshipMapper.approveScholarship(scholarship.getUser_id(),scholarship.getAdvice(),scholarship.getIs_yes());
            results.setCode(100);
        }catch (Exception e){
            results.setMsg("操作异常，请重试");
            e.printStackTrace();
        }
        return results;
    }
}
